﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Logger;
using Cofe.Core.Property;
using Cofe.Core.Utils;

namespace Cofe.Core.Entry
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class EntryListRegistrar : EntryListRegistrarBase
    {
        #region Constructor

        public EntryListRegistrar()
        {
        }

        #endregion

        #region Methods

    
        protected override T register<T>(IPropertyProvider entryListPP)
        {
            var entryListPH = CofeServices.EntryConstructor.ConstructPropertyHost(entryListPP);
            var entryList = CofeServices.EntryConstructor.ConstructEntry<T>(entryListPH);

            if (entryList == null)
                throw new ArgumentException();

            lock (_entryLists)
                if (!_entryLists.ContainsKey(entryList.Name))
                    _entryLists.Add(entryList.Name, entryList.Properties);
                else _entryLists[entryList.Name] = entryList.Properties;

            return entryList ;
        }

        public override IPropertyHost Find(string name)
        {
            lock (_entryLists)
                if (_entryLists.ContainsKey(name))
                    return _entryLists[name];
            return null;
        }

        public override void Remove(string name)
        {
            lock (_entryLists)
                if (_entryLists.ContainsKey(name))
                    _entryLists.Remove(name);
        }

        #endregion

        #region Data

        Dictionary<string, IPropertyHost> _entryLists = new Dictionary<string, IPropertyHost>();

        #endregion

        #region Public Properties      

        #endregion

    }

    public abstract class EntryListRegistrarBase : IEntryListRegistrar
    {
        #region Constructor

        public EntryListRegistrarBase()
        {
        }

        #endregion

        #region Methods

        public ICustomEntryList CreateCustomEntryList(string name = null, bool isRoot = false)
        {
            if (name == null)
                name = "Unnamed-";
            
            if (!isRoot) 
                name += "-" + StringUtils.RandomString(20);
            
            return register<ICustomEntryList>(new CustomEntryListPrimaryPP(name, isRoot));
        }

        public IAutoEntryList CreateAutoEntryList(string filterStr)
        {
            return register<IAutoEntryList>(new AutoEntryListPropertyProvider(filterStr));
        }

        public IAutoEntryList CreateAutoEntryList(Filter.IEntryFilter[] entryFilters, Filter.IOptionFilter[] optionFilters)
        {
            return register<IAutoEntryList>(new AutoEntryListPropertyProvider(entryFilters, optionFilters));
        }

        protected abstract T register<T>(IPropertyProvider entryListPP) where T : IEntryList;        
        public abstract IPropertyHost Find(string name);
        public abstract void Remove(string name);

        #endregion

        #region Data

     

        #endregion

        #region Public Properties

        #endregion

    }
}
